<div style="position: absolute;top: 120px; left: 450px">
    <font style="font-size: 25px ;color: #e8198b">采购单修改</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" class="su" value="添加明细" onclick="addItem()">
    <input type="button" class="su" value="保存" onclick="updatePoMain2()">
</div>


<br>
<table class="altrowstable" id="userListDiv">
    <tr>
        <td>采购单编号</td>
        <td><input type="text" id="POID" readonly="true" class="border-item"></td>
        <td>创建时间</td>
        <td><input type="text" id="createDate" readonly="true" class="border-item"></td>
        <td>供应商</td>
        <td id="vender"></td>
    </tr>
    <tr>
        <td>创建用户</td>
        <td><input type="text" id="account" class="border-item" readonly="true"></td>
        <td>附加费用</td>
        <td><input type="text" id="tipFee" onchange="productTotal()" value="0" class="border-item"></td>
        <td>采购产品总价</td>
        <td><input type="text" id="productTotal" readonly="true" class="border-item"></td>
    </tr>
    <tr>
        <td>付款方式</td>
        <td><select id="payType" class="border-item" onchange="getPrePay()">
            <option>货到付款</option>
            <option>款到发货</option>
            <option>预付款发货</option>
        </select></td>
        <td>最低预付款金额</td>
        <td><input type="hidden" id="prePayFee" class="border-item"value="0" ></td>
        <td>备注</td>
        <td><input type="text" id="remark" class="border-item"></td>
    </tr>
</table>
<table class="poItemList" id="PoItem"  style="border: 2px" >
    <tr>
        <td>序号</td>
        <td>产品编号</td>
        <td>产品名称</td>
        <td>数量单位</td>
        <td>产品数量</td>
        <td>采购单价</td>
        <td>采购总价</td>
        <td>删除</td>
    </tr>
    <tbody id="item"></tbody>
</table>
<script type="text/html" id="venders" >
    <select class="border-item" id="venderName">
        {{each data}}
        <option value="{{$value.venderCode}}">{{$value.name}}</option>
        {{/each}}
    </select>
</script>
<script type="text/html" id="items">
    {{each data}}
        <tr>
            <td>{{$index+1}}</td>
            <td>{{$value.productCode}}</td>
            <td>{{$value.productName}}</td>
            <td>{{$value.unitName}}</td>
            <td><input class="border-item" onchange="productTotal()" value={{$value.num}}></td>
            <td><input  class="border-item" onchange="productTotal()" value={{$value.unitPrice}}></td>
            <td>{{$value.itemPrice}}</td>
            <td onclick='deleItem(this)' style='color: red'>删除</td>
        </tr>
    {{/each}}
</script>
<script>

    function getPrePay(){
        var payType =$('#payType').find("option:selected").val();
        if (payType=="预付款发货"){
            $('#prePayFee').attr("type","text");
        }else {
            $('#prePayFee').attr("type","hidden");
        }
    }

    $(function () {
        Ajax.post("getUpdateItem",{
            poID:$('#POID').val()
        },function (reslut) {
            var str=template("items",reslut);
            document.getElementById("item").innerHTML=str;
        });
        Ajax.post("getVender",{},
            function (result) {
                var venders=template("venders",result);
                document.getElementById("vender").innerHTML=venders;
            });

        Ajax.post("updatePoMain1",{
            poID:$('#POID').val()
        },function (reslut) {
            var payType=document.getElementById("payType");
            for (var i=0;i<payType.options.length;i++){
                if (payType.options[i].text==reslut.data.payType){
                    payType.options[i].selected=true;
                    break;
                }
            }
            var vender =document.getElementById("venderName");
            for (var i=0;i<vender.options.length;i++){
                if (vender.options[i].value==reslut.data.venderCode){
                    vender.options[i].selected=true;
                }
            }
        })

    });

    var price=0.0;
    function productTotal() {
        var tr=$("#PoItem tr");
        var total=0.0;

        for (var i=1;i<tr.length;i++){
            var td= $(tr[i]).find("td");
            if (td.length>0){
                var pr= $(td[5]).find("input").val();
                var num = $(td[4]).find("input").val();
                $(td[6]).html(pr*num);
                total= total+ parseFloat($(td[6]).html());

            }

        }
        price=parseFloat(total);
        total=price+parseFloat($('#tipFee').val());
        $('#productTotal').val(total);
    }
    function updatePoMain2() {
        var total=0.0;
        var tr=$("#PoItem tr");
        for (var i=1;i<tr.length;i++){
            var td= $(tr[i]).find("td");
            if (td.length>0){
                var pr= $(td[5]).find("input").val();
                var num = $(td[4]).find("input").val();
                $(td[6]).html(pr*num);
                total= total+ parseFloat($(td[6]).html());

            }

        }
        var tr=$('#PoItem tr');
        var item=[];
        for (var i=1;i<tr.length;i++){
            var tds=$(tr[i]).find("td");
            if (tds.length>0){
                item.push({
                    poID:$('#POID').val(),
                    productCode:$(tds[1]).html(),
                    unitPrice:$(tds[5]).find("input").val(),
                    num:$(tds[4]).find("input").val(),
                    unitName:$(tds[3]).html(),
                    itemPrice:$(tds[6]).html()
                })
            }
        }

        var poMain={
            poID:$('#POID').val(),
            venderCode:$('#venderName').find("option:selected").val(),
            account:$('#account').val(),
            createTime:$('#createDate').val(),
            tipFee:$('#tipFee').val(),
            productTotal:total,
            poTotal:$('#productTotal').val(),
            payType:$('#payType').find("option:selected").val(),
            prePayFee:$('#prePayFee').val(),
            remark:$('#remark').val(),
            items:item
        };
        $.ajax({
            url:"updatePoMain2",
            type:"post",
            data:JSON.stringify(poMain),
            dataType:'json',
            headers:{
                Accept:"application/json",
                "Content-type":"application/json"
            },
            success:function () {
                show('showPoMain.html')
            }
        })
    }




    function addItem() {
        Ajax.getHtml("addItem.html",function (html) {
            Ajax.post("/getProduct",{},function (result) {
                var proID = template("proID", result);
                document.getElementById("proIDBody").innerHTML=proID;
                var proName=template("proName",result);
                document.getElementById("proNameBody").innerHTML=proName;
            });

            layer.open({
                type:1,
                title:'采购单明细添加',
                area: ['800px',['400px']],
                content: html,
                btn: ['确认', '取消'],
                yes :
                    function () {
                        var productID = $('#productID').find("option:selected").val();
                        var productName =$('#productName').find("option:selected").val();
                        var untilName=$('#proUnit').val();
                        var num=$('#num').val();
                        var price=$('#price').val();
                        var total=$('#total').val();
                        var tr = $('#PoItem tr');
                        var count=1;
                        for (var i=1;i<tr.length;i++){
                            var tds=$(tr[i]).find("td");
                            var Pid=$(tds[1]).html();
                            var preNum=parseFloat($(tds[4]).find("input").val());
                            if (Pid==productID){
                                count=2;
                                $(tds[5]).find("input").val(price);
                                $(tds[4]).find("input").val(parseFloat(num)+preNum);
                                layer.closeAll();
                                productTotal()
                                break;
                            }

                        }
                        if (count==1){
                            var tr =$("<tr></tr>");
                            tr.html("<td><span class='order'>"+ rowCount+"</span></td><td>"+productID+"</td><td>"+productName+"</td><td>"+untilName+"</td><td><input class=\"border-item\" onchange=\"productTotal()\" value='"+num+"'></td><td><input  class=\"border-item\" onchange=\"productTotal()\" value='"+price+"'></td> <td>"+total+"</td> <td onclick='deleItem(this)' style='color: red'>删除</td>");
                            $('#PoItem').append(tr);
                            layer.closeAll();
                            productTotal()
                        }


                    }
            })
        })};

    function deleItem(Obj) {
        var flag= window.confirm("确认删除");
        if (flag){
            var tr=$(Obj).parents("tr");
            tr.remove();
            var order= $('.order');
            for (var i=0;i<order.length;i++){
                order[i].innerHTML=i+1;
            }
        } else {return false}

    }


</script>